package cn.iocoder.yudao.module.picturebook.service.login;

import cn.iocoder.yudao.module.picturebook.controller.admin.login.vo.LoginResponse;
import cn.iocoder.yudao.module.picturebook.dal.dataobject.user.PBUserDO;
import cn.iocoder.yudao.module.picturebook.dal.mysql.user.PBUserMapper;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private PBUserMapper pBUserMapper;

    /**
     * 验证用户登录信息
     *
     * @param username 用户名，用于尝试登录
     * @param password 密码，用于尝试登录
     */
    @Override
    public LoginResponse authenticateUser(String username, String password) {
        //用userMapper中的方法验证登入
        //先验证账号是否存在
        //再验证密码
        //验证成功返回token

        if (password == null || username == null) {
            return new LoginResponse(null);
        }
        PBUserDO user = pBUserMapper.selectByUsername(username);
        if (user == null) {
            return new LoginResponse(null);
        }
        if (!password.equals(user.getPassword())) {
            return new LoginResponse(null);
        }


        // 基于用户详细信息生成认证令牌
        String token = generateToken(user.getId());

        // 返回包含认证令牌的响应对象
        return new LoginResponse(token);
    }


    private String generateToken(Long UserId) {
        return String.valueOf(UserId);
    }

}    